/*
 * @Author: Marte
 * @Date:   2016-10-25 11:13:49
 * @Last Modified by:   Marte
 * @Last Modified time: 2016-10-31 09:26:59
 */

'use strict';

var sequelizeService = require('../utils/sequelizeService');

var TypeRemark = sequelizeService.models.TypeRemark;

var _ = require('underscore');
var Promise = require('bluebird');

var controller = {

};

/**
 * [getTypeByRemark 根据评语获取类型]
 * @param  {[type]}   req  [description]
 * @param  {[type]}   res  [description]
 * @param  {Function} next [description]
 * @return {[type]}        [Json]
 */
controller.getTypeByRemark = function(req, res, next) {
    var remarkId = req.query.remarkid;
    var getTypes = TypeRemark.findTypeByRemark(remarkId);
    if (!remarkId) {
        return res.status(400).json({
            error: '错误的参数！',
            syserror: err.message
        });
    } else {
        getTypes
            .then(function(types) {
                return res.status(200).json(types);
            })
            .error(function(err) {
                return res.status(400).json({
                    error: '读取数据出错！',
                    syserror: err.message
                });
            })
            .catch(function(err) {
                return res.status(500).json({
                    error: '读取数据出错！',
                    syserror: err.message
                });
            });
    }
};

/**
 * [getRemarkByType 根据类型获取评语]
 * @param  {[type]}   req  [description]
 * @param  {[type]}   res  [description]
 * @param  {Function} next [description]
 * @return {[type]}        [Json]
 */
controller.getRemarkByType = function(req, res, next) {
    var typeId = req.query.typeid;
    var getRemarks = TypeRemark.findRemarkByType(typeId);
    console.log(typeId);
    if (!typeId) {
        return res.status(400).json({
            error: '错误的参数！',
            syserror: err.message
        });
    } else {
        getRemarks
            .then(function(remarks) {
                return res.status(200).json(remarks);
            })
            .error(function(err) {
                return res.status(400).json({
                    error: '读取数据出错！',
                    syserror: err.message
                });
            })
            .catch(function(err) {
                return res.status(500).json({
                    error: '读取数据出错！',
                    syserror: err.message
                });
            });
    }
};

/**
 * [getJsonAll 获取所有证件类型及评语关系]
 * @param  {[type]}   req  [description]
 * @param  {[type]}   res  [description]
 * @param  {Function} next [description]
 * @return {[type]}        [description]
 */
controller.getJsonAll = function(req, res, next) {
    var getTypeRemarks = Type.fetch();

    getTypeRemarks
        .then(function(types) {
            return res.status(200).json(types);
        })
        .error(function(err) {
            return res.status(400).json({
                error: '读取数据出错！',
                syserror: err.message
            });
        })
        .catch(function(err) {
            return res.status(500).json({
                error: '读取数据出错！',
                syserror: err.message
            });
        });
};

controller.registerPath = function(router) {

    /*根据类型获取评语*/
    router.addPathGetHanlder('/api/typeremarks/remarks', controller.getRemarkByType);

    /*根据评语获取类型*/
    router.addPathGetHanlder('/api/typeremarks/types', controller.getTypeByRemark);

    /*获取所有类型和评语关系*/
    router.addPathGetHanlder('/api/typeremarks', controller.getJsonAll);

};

module.exports = controller;
